// 获取盲盒数据
const { blindBoxes } = require('../../data/dishes');

Page({
  data: {
    blindBoxes: [],
    selectedBox: null,
    showOrderSuccess: false
  },
  
  onLoad: function () {
    this.setData({
      blindBoxes
    });
  },
  
  // 点击盲盒
  onBoxClick: function(e) {
    const box = e.detail.box;
    this.setData({
      selectedBox: box
    });
  },
  
  // 选择盲盒
  onSelectBox: function(e) {
    const box = e.detail.box;
    
    // 如果已选中，则取消选中
    if (this.data.selectedBox && this.data.selectedBox.id === box.id) {
      this.setData({
        selectedBox: null
      });
    } else {
      // 否则选中
      this.setData({
        selectedBox: box
      });
    }
  },
  
  // 下单
  placeOrder: function() {
    if (!this.data.selectedBox) return;
    
    const box = this.data.selectedBox;
    
    // 获取购物车
    const app = getApp();
    const cart = app.globalData.cart || [];
    
    // 添加到购物车
    cart.push({
      id: `blindbox_${box.id}`,
      name: box.name,
      price: box.price,
      image: box.image,
      description: box.description,
      quantity: 1,
      isBlindBox: true
    });
    
    // 更新全局数据
    app.globalData.cart = cart;
    
    // 更新本地存储
    wx.setStorageSync('cart', cart);
    
    // 显示下单成功弹窗
    this.setData({
      showOrderSuccess: true
    });
  },
  
  // 继续购物
  continueShopping: function() {
    this.setData({
      showOrderSuccess: false
    });
    
    // 返回首页
    wx.switchTab({
      url: '/src/pages/index/index'
    });
  },
  
  // 查看购物车
  goToCart: function() {
    this.setData({
      showOrderSuccess: false
    });
    
    // 跳转到购物车页面
    wx.switchTab({
      url: '/src/pages/cart/cart'
    });
  }
}) 